import { useStorage } from "@vueuse/core"

const TokenKey = "Admin-Token"
const tokenStorage = useStorage<null | string>(TokenKey, null)

export const getToken = (): string | null => {
  const token = tokenStorage.value
  return token
}

export const setToken = async (access_token: string): Promise<void> => {
  tokenStorage.value = access_token
  // 确保写入完成
  await new Promise(resolve => setTimeout(resolve, 0))
}

export const removeToken = (): void => {
  tokenStorage.value = null
}
